package controller;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.*;
import java.util.HashMap;
import java.util.Map;

public class ActivitiController2 {

    public static void main(String[] args) {

        ProcessEngineConfiguration processEngineConfiguration
                = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        //提交任务，开始执行
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test5");
        processEngineConfiguration.setJdbcUsername("root");
        processEngineConfiguration.setJdbcPassword("");
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        TaskService taskService = processEngine.getTaskService();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Deployment deploy = repositoryService.createDeployment().addClasspathResource("myProcess.bpmn").deploy();
        Map<String,Object> map =new HashMap<String, Object>();
        //界面ID
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("re_attend", map);
        System.out.println("id:"+processInstance.getId());
      //activitid:my_user激活了请假人
        System.out.println("activitid:"+processInstance.getActivityId());
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("id:"+task.getId());
        System.out.println("taskName:"+task.getName());
        map.put("attend_morning","09:00");
        map.put("attend_evening","18:30");
//        //提交,到下个任务
        taskService.complete(task.getId(),map);

        //查看有哪些任务需要审批
        //my_name为到管理的，组长审批
            List<Task> list = taskService
                    .createTaskQuery().taskDefinitionKey("my_name").active().list();
            Map<String, Object> variables = taskService.getVariables(list.get(0).getId());
            for (Task task1:list){
                Task task2 = taskService.createTaskQuery().taskId(task1.getId()).singleResult();
                taskService.complete(task1.getId());
            }


    }
}
